package com.google.code.sapwcrawler.data;
import java.util.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class DownloadedData
{	
	private Map<String,List<String>> headers;
	private URL from;
	private byte[] data;
	
	public DownloadedData(byte[] data,Map<String,List<String>> headers,URL from)
	{
		this.data = data;
		this.headers = headers;
		this.from = from;
	}
	
	public String getHeaderField(String key)
	{
		return getHeaderField(key,0);
	}
	
	public String getHeaderField(String key,int pos)
	{
		if(headers.containsKey(key))
			if(headers.get(key).size() > pos)
				return headers.get(key).get(pos);
		return null;
	}
	
	public String getContentType()
	{
		return getHeaderField("Content-type");
	}
	
	public URL getFrom()
	{
		return from;
	}
	
	public byte[] getData()
	{
		return data;
	}
	
	public String getContentEncoding()
	{
		return "utf-8";
	}
	
	public synchronized String toHtml()
	{
		return toHtml(getContentEncoding());
	}
	
	public synchronized String toHtml(String charset)
	{
		return Charset.forName(charset).decode(ByteBuffer.wrap(data)).toString();
	}
}
